package planner.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import planner.dao.UserDAO;
import planner.domain.User;
import planner.exception.UserDuplicateException;
import planner.exception.UserNotExistException;

public class UserServiceDo implements UserService {
	private final Log logger = LogFactory.getLog(UserServiceDo.class);
	
	private UserDAO userDAO;
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	@Override
	public void join(User user) throws UserDuplicateException {
		if(!isExistUser(user)){
			userDAO.insertUser(user);
		} else {
			if (logger.isWarnEnabled()) {
				logger.warn("userid inputted exists in DB :" + user.getUserId());
			}
			throw new UserDuplicateException();
		}
	}

	@Override
	public boolean isUserValid(User user) throws UserNotExistException {
		Boolean isValid = userDAO.isUserValid(user);
		if(isValid == null) {
			if (logger.isWarnEnabled()) {
				logger.warn("such userid dose not exist in DB :" + user.getUserId());
			}
			throw new UserNotExistException();
		} else {
			if (logger.isInfoEnabled()) {
				logger.info("user exists in DB");
			}
			return isValid;
		}
	}

	@Override
	public boolean isExistUser(User user) {
		if(userDAO.isExistUser(user)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public User getUser(User user) {
		return userDAO.getUser(user);
	}
}
